<template>
  <el-card class="container-card">
    <el-button
      type="primary"
      icon="el-icon-plus"
      :disabled="!category3Id"
      @click="$emit('updateShow', 2)"
    >添加SPU
    </el-button>
    <el-table
      v-loading="loading"
      :data="SpuList"
      border
      class="container-table"
    >
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-tooltip effect="dark" content="新增SKU" placement="top-start">
            <el-button
              type="warning"
              icon="el-icon-plus"
              size="mini"
              @click="addSku(row.id)"
            />
          </el-tooltip>
          <el-tooltip effect="dark" content="修改SPU" placement="top-start">
            <el-button
              type="danger"
              icon="el-icon-edit"
              size="mini"
            />
          </el-tooltip>
          <el-tooltip effect="dark" content="查看SKU列表" placement="top-start">
            <el-button type="info" icon="el-icon-info" size="mini" />
          </el-tooltip>
          <el-popconfirm
            class="spu-delete-popconfirm"
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @onConfirm="deleteSpuById(row.id)"
          >
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              size="mini"
            />
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[3, 6, 9, 12]"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next, jumper, total, sizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script>
import { reqSpuList, reqDeleteSpuById } from '@/api/product/spu'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'SpuList',
  data() {
    return {
      SpuList: [],
      currentPage: 1,
      pageSize: 3,
      total: 0,
      loading: false
    }
  },
  computed: {
    ...mapState('category', ['category3Id'])
  },
  watch: {
    category3Id: {
      handler(category3Id) {
        if (!category3Id) {
          this.SpuList = []
          this.currentPage = 1
          this.pageSize = 3
          this.total = 0
          return
        }
        this.getSpuList()
      },
      immediate: true
    }
  },
  methods: {
    ...mapMutations('spu', ['SET_SPU_ID']),
    handleSizeChange(pgaeSize) {
      this.pageSize = pgaeSize
      this.getSpuList()
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.getSpuList()
    },
    // 获取Spu列表
    async getSpuList() {
      this.loading = true
      const { currentPage, pageSize } = this
      const res = await reqSpuList({
        page: currentPage,
        limit: pageSize,
        category3Id: this.category3Id
      })
      this.SpuList = res.records
      this.total = res.total
      this.loading = false
    },
    // 新增SKU
    addSku(spuId) {
      this.SET_SPU_ID(spuId)
      this.$emit('updateShow', 3)
    },
    // 删除单条Spu
    async deleteSpuById(spuId) {
      await reqDeleteSpuById(spuId)
      this.$message.success('删除成功')
      this.getSpuList()
    }
  }
}
</script>

<style scoped>
.spu-delete-popconfirm {
  margin-left: 10px;
}
</style>
